﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using PesticideManage.API.Services.Files;
using PesticideManage.API.Services.Files.Models;
using Shang.COSXML;
using Shang.COSXML.Extensions.DependencyInjection;

namespace PesticideManage.API.Services.Files.Extensions.DependencyInjection;

public static class ServiceCollectionExtensions
{
    public static void AddStoreService(this IServiceCollection services, IConfiguration configuration)
    {
        services.Configure<StoreConfig>(configuration.GetSection(nameof(StoreConfig)));
        //var c = builder.Configuration.GetSection(nameof(StoreConfig)).Get<StoreConfig>();
        services.AddCosXml(configuration.GetSection(CosXmlConfigOptions.Name));
        services.AddScoped<TencentCosService>();
        services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
        services.AddScoped<LocalStoreService>();
        services.AddScoped<IStoreService>(sp =>
        {
            var storeConfig = sp.GetRequiredService<IOptionsSnapshot<StoreConfig>>().Value;

            if (storeConfig.Source == StoreSource.TencentCos)
            {
                return sp.GetRequiredService<TencentCosService>();
            }
            else
            {
                return sp.GetRequiredService<LocalStoreService>();
            }
            /*return storeConfig.Source switch
            {
                StoreSource.TencentCos => sp.GetRequiredService<TencentCosService>(),
                _ => sp.GetRequiredService<LocalStoreService>(),
            };*/
        });

    }
}
